package com.situ.cart.api.api;


import com.mall.common.util.JsonResult;
import com.situ.cart.api.service.ProductFeignService;
import com.situ.cart.spi.model.CartItem;
import com.situ.cart.spi.service.CartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(value = "/api/cartItems", produces = MediaType.APPLICATION_JSON_VALUE)
public class CartItemApi {
    private CartItemService cartItemService;
    private ProductFeignService productFeignService;

    @Autowired
    public void setCartItemService(CartItemService cartItemService) {
        this.cartItemService = cartItemService;
    }

    @Autowired
    public void setProductFeignService(ProductFeignService productFeignService) {
        this.productFeignService = productFeignService;
    }

    //根据会员编号查询指定会员的所有购物车项
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(Integer UserId) {
        List<CartItem> items = cartItemService.findByUserId(UserId);
        return ResponseEntity.ok(JsonResult.success(items));
    }

    //根据购物车项编号查询唯一购物车项
    @GetMapping("/id/{id}")
    public ResponseEntity<JsonResult<?>> findById(@PathVariable Integer id) {
        CartItem item = cartItemService.findById(id);
        if (item == null) {
            return ResponseEntity.ok(JsonResult.fail("购物车项不存在"));
        } else {
            return ResponseEntity.ok(JsonResult.success(item));
        }
    }

    @PostMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody CartItem item) {
        boolean success = cartItemService.save(item);
        if (success) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("保存购物车项失败"));
        }
    }

    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody CartItem item) {
        boolean success = cartItemService.update(item);
        if (success) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改购物车项失败"));
        }
    }

    //根据购物车项的ids删除购物车项
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteByIds(@RequestBody Integer[] ids) {
        Objects.requireNonNull(ids);
        int count = cartItemService.deleteByIds(List.of(ids));
        if (count > 0) {
            return ResponseEntity.ok(JsonResult.success(count));
        } else {
            return ResponseEntity.ok(JsonResult.fail("购物车项删除失败"));
        }
    }

    //查询哪些用户的购物车里有该商品，返回用户ids;
    @GetMapping("/productId/{id}")
    public ResponseEntity<JsonResult<?>> findIdsByProductId(@PathVariable Integer id) {
        int[] ids = cartItemService.findByProductId(id);
        return ResponseEntity.ok(JsonResult.success(ids));
    }

    //根据用户id清除该用户的所有购物车项
    @DeleteMapping("/clear/{userId}")
    public ResponseEntity<JsonResult<?>> clearCart(@PathVariable Integer userId) {
        int count = cartItemService.clearAllByUserId(userId);
        return ResponseEntity.ok(JsonResult.success("成功清除" + count + "件"));
    }
}
